﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Frame.Pages;
using DevExpress.Web.ASPxEditors;


public partial class EditConsumerIntegral : Frame.Pages.FliterBIEditBasePage<DAL.BLI_ConsumerIntegral>
{
    protected override Func<DAL.BLI_ConsumerIntegral, bool> FliterExpression
    {
        get
        {
            if (RefrenceContext<DAL.BI_Consumer>.Current != null)
            {
                return new Func<BLI_ConsumerIntegral, bool>(o => o.Consumer_ID == RefrenceContext<DAL.BI_Consumer>.Current.Model.ConsumerID);
            }
            else
            {
                return null;
            }
        }
    }

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (!IsPostBack && !IsCallback)
        {
            if (RefrenceContext<DAL.BI_Consumer>.Current != null)
            {
                Backlink.NavigateUrl = (RefrenceContext<DAL.BI_Consumer>.Current == null ? string.Empty : RefrenceContext<DAL.BI_Consumer>.Current.BackUrl);
            }
        }
        GetPoint();
    }

    void GetPoint()
    {
        int cid = Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current != null ? Frame.Pages.RefrenceContext<DAL.BI_Consumer>.Current.Model.ConsumerID : -1;
        int Has;
        int Not;
        object o;
        //总体情况
        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where  Type=1 and   Consumer_ID=" + cid);
        if (o == DBNull.Value)
        {
            Has = 0;
        }
        else
        {
            Has = (int)o;
        }

        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where Consumer_ID=" + cid);
        if (o == DBNull.Value)
        {
            Not = 0;
        }
        else
        {
            Not = (int)o;
        }
        View_Cumulative.Text = (System.Math.Abs(Has) + Not).ToString();
        View_Has.Text = System.Math.Abs(Has).ToString();
        View_Not.Text = Not.ToString();
        //年度情况
        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where  Type=1 and   Consumer_ID=" + cid + " and convert(varchar(50),AddTime,120) like '" + DateTime.Now.Year + "%'");
        if (o == DBNull.Value)
        {
            Has = 0;
        }
        else
        {
            Has = (int)o;
        }
        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where Consumer_ID=" + cid + " and convert(varchar(50),AddTime,120) like '" + DateTime.Now.Year + "%'");
        if (o == DBNull.Value)
        {
            Not = 0;
        }
        else
        {
            Not = (int)o;
        }
        View_YCumulative.Text = (System.Math.Abs(Has) + Not).ToString();
        View_YHas.Text = System.Math.Abs(Has).ToString();
        View_YNot.Text = Not.ToString();
        //本月情况
        string tm = DateTime.Now.ToString("yyyy-MM");
        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where  Type=1 and   Consumer_ID=" + cid + " and convert(varchar(50),AddTime,120) like '" + tm + "%'");
        if (o == DBNull.Value)
        {
            Has = 0;
        }
        else
        {
            Has = (int)o;
        }
        o = Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select SUM(IntegralNum) from dbo.BLI_ConsumerIntegral where Consumer_ID=" + cid + " and convert(varchar(50),AddTime,120) like '" + tm + "%'");
        if (o == DBNull.Value)
        {
            Not = 0;
        }
        else
        {
            Not = (int)o;
        }
        View_MCumulative.Text = (System.Math.Abs(Has) + Not).ToString();
        View_MHas.Text = System.Math.Abs(Has).ToString();
        View_MNot.Text = Not.ToString();
    }

    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        //获取最新剩余积分
        GetPoint();
        if ((int)e.NewValues["IntegralNum"] > Convert.ToInt32(View_Not.Text))
            throw new Exception("积分不够!");

        e.NewValues["Consumer_ID"] = RefrenceContext<DAL.BI_Consumer>.Current.Model.ConsumerID;
        e.NewValues["ConsumerHis_id"] = "0";
        e.NewValues["Type"] = 1;
        e.NewValues["IntegralNum"] = (int)e.NewValues["IntegralNum"] * -1;
    }

    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        //获取最新剩余积分
        GetPoint();
        int newv = (int)e.NewValues["IntegralNum"];
        int oldv = (int)e.OldValues["IntegralNum"];
        if (newv > (Convert.ToInt32(View_Not.Text) + System.Math.Abs(oldv)))
            throw new Exception("积分不够!");

        if (oldv < 0)
            e.NewValues["IntegralNum"] = (int)e.NewValues["IntegralNum"] * -1;
    }
    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        GetPoint();
    }

    protected void gridView_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (this.gridView.IsEditing)
        {
            if (this.gridView.IsNewRowEditing)
            {
                if (e.Column.FieldName.ToString() == "Type")
                {
                    ASPxComboBox time = e.Editor as ASPxComboBox;
                    time.SelectedIndex = 2;
                    time.Enabled = false;
                }
            }
            else if (e.Column.FieldName.ToString() == "Type")
            {
                e.Editor.Enabled = false;
            }
        }
    }

    /// <summary>
    /// 权限编码，需要重新设置
    /// </summary>
    protected override string ActionID
    {
        get { return "0"; }
    }
}